//
//  NSDate+Category.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/5/12.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

extension NSDate{
    
    class func DateByStr(str: String) ->NSDate {
    
        let formatter: NSDateFormatter  = NSDateFormatter()
        formatter.dateFormat = "EEE MMM d HH:mm:ss Z yyy"
        formatter.locale = NSLocale(localeIdentifier: "cn")
        return formatter.dateFromString(str)!
    }
    
    
    var convertTime: String?{
          let calendar =  NSCalendar.currentCalendar()
           if calendar.isDateInToday(self) {
            
            //1.1、刚刚
           let timeINterval = Int(NSDate().timeIntervalSinceDate(self))
            if timeINterval < 60{
                  return "刚刚"
            }
            //1.2 几分钟前
            else if timeINterval < 60 * 60 {
                return "\(timeINterval/60)分钟前"
            }
            //1.3 几小时前
            return "\(timeINterval/(60 * 60))小时前"
        }
        
        let formatter = NSDateFormatter()
        //2 是不是昨天
        if calendar.isDateInYesterday(self){
            formatter.dateFormat = "昨天 HH:mm"
        }
        // 3 一年以内
       let unit =   calendar.components(NSCalendarUnit.Year, fromDate: self, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
        if unit.year < 1 {
            formatter.dateFormat = "MM-dd HH:mm"
        }
        // 更早时间
        else{
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        }
        formatter.locale = NSLocale(localeIdentifier: "cn")
        
         return formatter.stringFromDate(self)
        
    }
    
    
}
